﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using BE;
using BE.SearchCriteria;

namespace DAL
{
    public class CausaDevolucionDAL : BaseDAL
    {
        public List<CausaDevolucionBE> SeleccionarPorParametros(BaseSC pCausaDevolucionSC, int? pPageSize, int? pPageNum, ref int pCountReg)
        {
            CausaDevolucionSC wCausaDevolucionSC = (CausaDevolucionSC)pCausaDevolucionSC;
            if (wCausaDevolucionSC == null)
                wCausaDevolucionSC = new CausaDevolucionSC();

            List<CausaDevolucionBE> listaDeCausaDevolucions = null;
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cnn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@IdCausa", wCausaDevolucionSC.IdCausa));
                cmd.Parameters.Add(new SqlParameter("@Descripcion", wCausaDevolucionSC.Descripcion));
                
                SqlParameter p = new SqlParameter();
                p.ParameterName = "@PageCount";
                p.Direction = ParameterDirection.Output;
                p.SqlDbType = SqlDbType.Int;
                cmd.Parameters.Add(p);

                cmd.CommandText = "CausaDevolucions_GetAllPaginated";
                cnn.Open();
                SqlDataReader reader = null;
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    listaDeCausaDevolucions = new List<CausaDevolucionBE>();

                    while (reader.Read())
                    {
                        listaDeCausaDevolucions.Add(Construir(reader));
                    }
                }

                reader.Close();
                pCountReg = (Int32)cmd.Parameters["@PageCount"].Value;
            }
            return listaDeCausaDevolucions;
        }

        private CausaDevolucionBE Construir(SqlDataReader pReader)
        {
            CausaDevolucionBE oCausaDevolucion = new CausaDevolucionBE();

            oCausaDevolucion.IdCausa  = Convert.ToInt32(pReader["IdCausa"]);
            oCausaDevolucion.Descripcion = Convert.ToString(pReader["Descripcion"]);
           

            return oCausaDevolucion;
        }
    }
}
